package cn.hd01.web.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.hd01.common.BaseConstant.backendType;
import cn.hd01.model.ReportModel;
import cn.hd01.repository.entity.Permissions;
import cn.hd01.repository.entity.User;
import cn.hd01.service.COrderService;
import cn.hd01.service.PermissionsService;
import cn.hd01.service.RebateSummaryService;
import cn.hd01.service.UserService;
import cn.hd01.service.WithdrawBookService;
import cn.hd01.util.CommonUtil;
import cn.hd01.util.TreeNode;
import cn.hd01.web.auth.Auth;
import cn.hd01.web.auth.AuthType;
import cn.hd01.web.util.WebConstant;

@Controller
@Auth
public class LoginController {

	@Autowired
	private UserService service;
	
	@Autowired
	private COrderService cOrderService;
	
	@Autowired
	private RebateSummaryService rebateSummaryService;
	
	@Autowired
	private WithdrawBookService withdrawBookService;
	
	@Autowired
	private PermissionsService permissionsService;
	
	private static final Integer ROLE_ID = 11;
	
	@RequestMapping(path = { "", "/", "/index" }, method = RequestMethod.GET)
	public String index(Model m, String beginDate, String endDate, HttpServletRequest req) {
		User loginUser = (User) req.getSession().getAttribute(WebConstant.USER_SESSION_NAME);
		Integer type = CommonUtil.getLoginUserType(loginUser);
		
		if(backendType.TEACHER.equals(type)){
			return "redirect:/user/detail/" + loginUser.getId();
		}
		
		if(loginUser.getRoleId() == ROLE_ID){
			return "redirect:/cOrder/workReport" ;
		}
		
		TreeNode<Permissions> permissionsTree = permissionsService.findMenuByRoleId(loginUser.getRoleId());
		List<TreeNode<Permissions>> nodeList = permissionsTree.getChildren();
		TreeNode<Permissions> node = nodeList.get(0);
		
		Permissions p = node.getData();
		List<TreeNode<Permissions>> nodeList2 = node.getChildren();
		
		return "forward:" + nodeList2.get(0).getData().getUrl();
		
		//return "index";
	}
	
	private String url(String menu, String basepath) {
		if (!StringUtils.hasText(menu)) {
			return "javascript:void(0);";
		}

		return basepath + (menu.startsWith("/") ? "" : "/") + menu;
	}
	
	@ResponseBody
	@RequestMapping(path = "/reportData", method = RequestMethod.POST)
	public ReportModel getReportData(Model m, String beginDate, String endDate, HttpServletRequest req){
		List<Object[]> orderSummaryList = cOrderService.findSummary(beginDate, endDate);
		BigDecimal allAmount = BigDecimal.ZERO;
		BigDecimal allRebate = BigDecimal.ZERO;
		BigDecimal allWaitWithdrawRebate = BigDecimal.ZERO;
		if(!CollectionUtils.isEmpty(orderSummaryList)){
			Object[] arr = orderSummaryList.get(0);
			try{
				allAmount = arr[0] == null ? BigDecimal.ZERO : (BigDecimal) arr[0];
				allRebate = arr[1] == null ? BigDecimal.ZERO : (BigDecimal) arr[1];
				allWaitWithdrawRebate = arr[2] == null ? BigDecimal.ZERO : (BigDecimal) arr[2];
			}catch(Exception e){}
		}
		
		ReportModel model = new ReportModel();
		model.setAllAmount(allAmount);
		model.setAllRebate(allRebate);
		
		BigDecimal allWithdrawRebate = withdrawBookService.findWithdrawReabate(beginDate, endDate);
		model.setAllWithdrawRebate(allWithdrawRebate == null ? BigDecimal.ZERO : allWithdrawRebate);
		model.setAllWaitWithdrawRebate(allWaitWithdrawRebate);

		return model;
	}

	@Auth(AuthType.OFF)
	@RequestMapping(path = WebConstant.LOGIN_URL, method = RequestMethod.GET)
	public String login() {
		return "login";
	}

	@RequestMapping(path = "/logout", method = RequestMethod.GET)
	public String logout(HttpSession session) {
		session.removeAttribute(WebConstant.USER_SESSION_NAME);
		return "redirect:" + WebConstant.LOGIN_URL;
	}

	@Auth(AuthType.OFF)
	@RequestMapping(path = WebConstant.LOGIN_URL, method = RequestMethod.POST)
	public ModelAndView toLogin(User u, HttpSession session) {
		User user = service.find(u.getName(), u.getPassword());

		if (user != null) {
			session.setAttribute(WebConstant.USER_SESSION_NAME, user);
			return new ModelAndView("redirect:/index");
		}

		return new ModelAndView("login", "message", "用户名或者密码错误！");
	}
}
